#include <iostream>
#include <queue>
#define MAXMN 105
using namespace std;
int N, M;
int dxdy[][2] = {
    {-1, 0}, {1, 0},
    {0, -1}, {0, 1}
};

int sx, sy, ex, ey;

struct State {
    pair<int, int> pos;
    int step;
};

char field[MAXMN][MAXMN];

void solve();

int main() {
    cin >> N >> M;
    for (int i = 1; i <= N; i++) {
        for (int j = 1; j <= M; j++) {
            cin >> field[i][j];
            if (field[i][j] == 'S')
                sx = i, sy = j;
            if (field[i][j] == 'G')
                ex = i, ey = j;
        }
    }
    solve();
}

/*
#S######.#
......#..#
.#.##.##.#
.#........
##.##.####
....#....#
.#######.#
....#.....
.####.###.
....#...G#

*/

void solve() {
    queue<State> q;
    q.push({{sx, sy}, 0});
    field[sx][sy] = '#';
    while (!q.empty()) {
        State current = q.front(); q.pop();
        if (current.pos.first == ex && current.pos.second == ey) {
            cout << current.step << endl;
            return;
        }
        for (int i = 0; i < 4; i++) {
            int nx = current.pos.first + dxdy[i][0],
                ny = current.pos.second + dxdy[i][1];
            if (field[nx][ny] != '#' && field[nx][ny] != '\0') {
                q.push({{nx, ny}, current.step + 1});
                field[nx][ny] = '#';
            }
        }
    }
}